<?php

namespace app\admin\controller;

class User extends Admin{

    //用户退出
    public function logOut(){
        session(null);
        return 1;
    }

    /**
     * 密码修改
     */
    public function setPassword(){
        if(IS_POST){
            $Users = M('users');
            $map = [
                'id' => session('user.id'),
                'password' => md5(I('post.dreambox')) 
            ];
            
            $res = $Users->where($map)->find();
            if(!$res){
                return '原始密码错误';
            }
            if(!preg_match("/^\w{6,20}$/", I('post.chpassword'))){
                return '新密码的长度为6-20个字符';
            }
            if(I('post.chpassword') != I('post.copassword')){
                return '两次密码输入不一致';
            }

            $data = [
                'password' => md5(I('post.chpassword')),
                'update_time' => I('post.update_time')
            ];

            $res = $Users->where(['id'=>session('user.id')])->setField($data);
            if($res){
                session('user',null);
            }
            return $res;
        }else{
            return $this->fetch('setPassword');
        }
    }
}